package org.crazyit.activiti_d4._2_GetProcessModel;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;

import java.io.InputStream;

public class GetProcessModel {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到流程存储服务对象
        RepositoryService repositoryService = engine.getRepositoryService();

        //3. 部署一份txt 文件
        Deployment dep = repositoryService.createDeployment().addClasspathResource("getProcessModel.bpmn").deploy();

        //4. 查询流程定义
        ProcessDefinition def = repositoryService.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();

        //5. 查询资源文件
        InputStream is = repositoryService.getProcessModel(def.getId());

        try {

            //6. 读取输入流
            int count = is.available();
            byte[] contents = new byte[count];
            is.read(contents);
            String result = new String(contents);

            //7. 输出结果
            System.out.println(result);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}
